package fr.esiag.isidis.fi.pdsTeam.injector;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;


import fr.esiag.isidis.fi.pdsTeam.events.MyEvent;
import fr.esiag.isidis.fi.pdsTeam.events.RawEvent;
import fr.esiag.isidis.fi.pdsTeam.interfaces.IInjector;
import fr.esiag.isidis.fi.pdsTeam.swing.PrincipalFrame;
import fr.esiag.isidis.fi.pdsTeam.tram.Marshaller;

public class Injector implements IInjector {
	
	private byte[] tram;
	

	public void demarrerInjector() {
		PrincipalFrame frame = new PrincipalFrame(this);
	}


	public void sendMessage(byte[] trame) {
		
		try{
			InetAddress host_address = InetAddress.getByName("localhost");
			DatagramSocket socket = new DatagramSocket();
			DatagramPacket paquet = new DatagramPacket(trame, trame.length,host_address,1010);
			socket.send(paquet);
			socket.close();
		}catch(Exception e){
		    System.err.println("Erreur: "+e);
		}
	}


	public void instancierEvent(Integer codeEvent, String macAdd) {
		RawEvent event = new RawEvent();
		event.setEventCode(codeEvent);
		event.setSensorMacAddress(macAdd);
		tram = Marshaller.getInstance().marsahllEvent((MyEvent) event);
		sendMessage(tram);
	}

}
